package it.gat.controller.resource;

import it.gat.controller.utils.GatSession;
import it.gat.controller.utils.WSImpostazioni;
import org.restlet.data.ChallengeResponse;
import org.restlet.data.ChallengeScheme;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;

public class ResourceFactory{
	
	public static <T> T get(String resource,String value,Class<T> clazz)
	{	
		String webserviceUrl = WSImpostazioni.getWebserviceUrl();
		String webservicePort = WSImpostazioni.getWebservicePort(); 
		String webserviceUri = WSImpostazioni.getWebserviceUri();
		
		String uri = "http://" + webserviceUrl + ":" + 
				webservicePort + "/" +
				webserviceUri +
		             resource +"/" + value;
		System.out.println("Get from: "+uri);
		ClientResource cr = new ClientResource(uri);
		ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; 
		ChallengeResponse authentication = new ChallengeResponse(scheme, GatSession.getInstance().getNomeUtente(), GatSession.getInstance().getPassword()); 
		cr.setChallengeResponse(authentication);
		T response = cr.get(clazz);
		cr.release();
		return response;
	}
	
	public static <T> T get(String resource,int id,Class<T> clazz)
	{
		return ResourceFactory.get(resource, Integer.toString(id), clazz);
	}
	
	public static <T> T post(String resource,Object object,Class<T> clazz)
	{	
		String webserviceUrl = WSImpostazioni.getWebserviceUrl();
		String webservicePort = WSImpostazioni.getWebservicePort(); 
		String webserviceUri = WSImpostazioni.getWebserviceUri();
		
		String uri = "http://" + webserviceUrl + ":" + 
				webservicePort + "/" +
				webserviceUri +
		             resource;
		System.out.println("Post to: "+uri);
		ClientResource cr = new ClientResource(uri);
		ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; 
		ChallengeResponse authentication = new ChallengeResponse(scheme, GatSession.getInstance().getNomeUtente(), GatSession.getInstance().getPassword()); 
		cr.setChallengeResponse(authentication);
		T response = cr.post(object,clazz);
		cr.release();
		return response;
	}
	
	public static void put(String resource,String value,Object object)
	{	
		String webserviceUrl = WSImpostazioni.getWebserviceUrl();
		String webservicePort = WSImpostazioni.getWebservicePort(); 
		String webserviceUri = WSImpostazioni.getWebserviceUri();
		
		String uri = "http://" + webserviceUrl + ":" + 
				webservicePort + "/" +
				webserviceUri +
		             resource +"/" + value;
		System.out.println("Put to: "+uri);
		ClientResource cr = new ClientResource(uri);
		ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; 
		ChallengeResponse authentication = new ChallengeResponse(scheme, GatSession.getInstance().getNomeUtente(), GatSession.getInstance().getPassword()); 
		cr.setChallengeResponse(authentication);
		Representation rapp = cr.put(object);
		cr.release();
	}
	
	public static void put(String resource,int value,Object object)
	{	
		ResourceFactory.put(resource, Integer.toString(value), object);
	}
	
	public static void delete(String resource,String value)
	{	
		String webserviceUrl = WSImpostazioni.getWebserviceUrl();
		String webservicePort = WSImpostazioni.getWebservicePort(); 
		String webserviceUri = WSImpostazioni.getWebserviceUri();
		
		String uri = "http://" + webserviceUrl + ":" + 
				webservicePort + "/" +
				webserviceUri +
		        resource +"/" + value;;
		System.out.println("Delete: "+uri);
		ClientResource cr = new ClientResource(uri);
		ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; 
		ChallengeResponse authentication = new ChallengeResponse(scheme, GatSession.getInstance().getNomeUtente(), GatSession.getInstance().getPassword()); 
		cr.setChallengeResponse(authentication);
		Representation rapp = cr.delete();
		cr.release();
	}
	
	public static void delete(String resource,int value)
	{	
		ResourceFactory.delete(resource, Integer.toString(value));
	}
	
	
	
	

}
